<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="disinfectState">
              <el-select v-model="filters.disinfectState" :clearable="true" placeholder="消毒状态">
                <el-option v-for="item in disinfectStateList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="hospitalName" label="医院名称"></el-table-column>
        <el-table-column prop="officeName" label="科室名称"></el-table-column>
        <el-table-column prop="content" label="消毒器材"></el-table-column>
        <el-table-column prop="institutionName" label="消毒中心"></el-table-column>
        <el-table-column prop="syUserName" label="收样员"></el-table-column>
        <el-table-column prop="completeDate" label="期待完成时间"></el-table-column>
        <el-table-column prop="disinfectState" label="消毒状态" :formatter="formatDisinfectState" width="150"></el-table-column>
        <!--<el-table-column prop="state" label="状态" :formatter="formatState" width="100"></el-table-column>-->
        <el-table-column prop="createDate" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="240">
          //消毒状态：(1.待收取 2.收样员已收取 3.收样员已送达 4.消毒中心确认收到 // 5.消毒中心已消毒 6.收样员已回收 7.收样员已回送 8.医院确认已完成)
          <template slot-scope="scope">
            <!--<el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>-->
            <el-button v-has="permissionSetSyUser" v-if="scope.row.disinfectState == 1 && userRole == 1" @click="handleClickSetSyUser(scope.row)" size="mini">设置收样员</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 1 && userRole == 1" @click="handleClickUpdateState(scope.row, 2)" size="mini">收取</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 2 && userRole == 1" @click="handleClickUpdateState(scope.row, 3)" size="mini">送达</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 3 && userRole == 3" @click="handleClickUpdateState(scope.row, 4)" size="mini">确认收到</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 4 && userRole == 3" @click="handleClickUpdateState(scope.row, 5)" size="mini">已消毒</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 5 && userRole == 1" @click="handleClickUpdateState(scope.row, 6)" size="mini">已回收</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 6 && userRole == 1" @click="handleClickUpdateState(scope.row, 7)" size="mini">已回送</el-button>
            <el-button v-has="permissionState" v-if="scope.row.disinfectState == 7 && userRole == 2" @click="handleClickUpdateState(scope.row, 8)" size="mini">确认完成</el-button>
            <!--<el-dropdown trigger="click" size="small">
              <el-button type="primary">
                <i class="fa fa-list-ul"></i>更多
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{scope.row.state == 0? '启用' : '禁用'}}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state == 0" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>-->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <el-dialog title="设置收样员" :visible.sync="dialogVisible" :center="isCenter" width="400px">
      <el-form :model="messageForm" ref="messageForm">
        <el-form-item label="收样人员" prop="syUserId" label-width="100px">
          <el-select v-model="messageForm.syUserId" filterable clearable reserve-keyword :loading="loadingIns" placeholder="请选择收样人员">
            <el-option v-for="item in pickUserList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item align="center">
          <el-button type="primary" @click="sureMessageClick">保 存</el-button>
          <el-button @click="cancelMessageClick">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import DisinfectApi from '@/api/disinfect/disinfect';
import AuthApi from '@/api/system/auth';
import { getStorage } from '@/utils/auth';
import * as stn from '@/constants/storeName';

export default {
  name: 'DisinfectManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      filters: {
        disinfectState: '',
        state: '',
      },
      userInfo: {},
      userRole: '',
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      dataStatue: [
        { label: '禁用', value: '0' },
        { label: '启用', value: '1' },
      ],
      //消毒状态：(1.待收取 2.收样员已收取 3.收样员已送达 4.消毒中心确认收到
      // 5.消毒中心已消毒 6.收样员已回收 7.收样员已回送 8.医院确认已完成)
      disinfectStateList: [
        { label: '待收取', value: 1 },
        { label: '收样员已收取', value: 2 },
        { label: '收样员已送达', value: 3 },
        { label: '消毒中心确认收到', value: 4 },
        { label: '消毒中心已消毒', value: 5 },
        { label: '收样员已回收', value: 6 },
        { label: '收样员已回送', value: 7 },
        { label: '医院确认已完成', value: 8 },
      ],
      pickUserList: [],
      dialogVisible: false,
      isCenter: true,
      loadingIns: false,
      messageForm: {
        syUserId: '',
        disinfectId: '',
      },

      permissionAdd: 'SYSTEM_MANAGER_DISINFECT_INFO_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_SPECIMEN_LIST_DELETE',
      permissionSetSyUser: 'SYSTEM_MANAGER_DISINFECT_INFO_LIST_SET_SYUSER',
      permissionState: 'SYSTEM_MANAGER_DISINFECT_INFO_LIST_UPDATE_STATE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      this.userInfo = getStorage(stn.USER_INFO);
      // 根据用户角色，设置定好的查询条件
      if (this.userInfo) {
        this.userRole = this.userInfo.userRole;
      }
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
        this.listData();
      }
    },


    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    //消毒状态：(1.待收取 2.收样员已收取 3.收样员已送达 4.消毒中心确认收到
    // 5.消毒中心已消毒 6.收样员已回收 7.收样员已回送 8.医院确认已完成)
    formatDisinfectState(row, column, cellValue) {
      if (cellValue == 1) {
        return '待收取';
      } else if (cellValue == 2) {
        return '收样员已收取';
      } else if (cellValue == 3) {
        return '收样员已送达';
      } else if (cellValue == 4) {
        return '消毒中心确认收到';
      } else if (cellValue == 5) {
        return '消毒中心已消毒';
      } else if (cellValue == 6) {
        return '收样员已回收';
      } else if (cellValue == 7) {
        return '收样员已回送';
      } else if (cellValue == 8) {
        return '医院确认已完成';
      }
    },
    formatState(row, column, cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      } else if (cellValue == 2) {
        return '待审核';
      }
    },
    formatDate(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, FORMAT_DATE_YYYY_MM_DD);
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/disinfect/disinfectAdd');
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/disinfect/disinfectEdit',
        query: { id: row.disinfectId },
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/disinfect/disinfectDetail',
        query: { id: row.esId },
      });
    },
    listSyUser() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        roleId: 4,
      };
      AuthApi.listAdminByRole(cond)
        .then((res) => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.pickUserList = res.data.map((item) => {
              return {
                value: item.userId,
                label: item.userName,
              };
            });
          }, 200);
        })
        .catch((error) => {
          _this.pickUserList = [];
          console.error(error);
        });
    },
    handleClickSetSyUser(row) {
      let _this = this;
      _this.$Utils.clearData(_this.messageForm);
      _this.dialogVisible = true;
      _this.messageForm.disinfectId = row.disinfectId;
      _this.listSyUser();
    },
    sureMessageClick() {
      let _this = this;
      DisinfectApi.editDisinfect(_this.messageForm).then((data) => {
        this.dialogVisible = false;
        _this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS,
        });
        _this.listData();
      });
    },
    cancelMessageClick() {
      this.dialogVisible = false;
    },
    handleClickUpdateState(row, state) {
      let _this = this;
      this.$confirm('确认执行此操作吗？', message.TITLE_PROMPT, {}).then(() => {
        DisinfectApi.updateDisinfectState({
          disinfectId: row.disinfectId,
          disinfectState: state,
        }).then((res) => {
          _this.$notify.success({
            title: message.TITLE_SUCCESS,
            message: message.MSG_ACTION_SUCCESS,
          });
          _this.listData();
        });
      });
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state == 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        DisinfectApi.deleteDisinfect({
          esId: row.esId,
          state: currentState,
        }).then((res) => {
          _this.listData();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        DisinfectApi.deleteDisinfect({
          esId: val.esId,
          state: -1,
        }).then((res) => {
          _this.listData();
          this.$message({
            type: 'success',
            message: message.MSG_ACTION_SUCCESS,
          });
        });
      });
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        disinfectState: _this.filters.disinfectState,
      };
      DisinfectApi.listDisinfect(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
